let goodsMethod = require("../dao/goodsListDAO").goodsMethod;
let fs = require("fs");

// 新增商品
exports.addGoods = function (req, res){
  goodsMethod.findOne({name:req.body.name}, function (err, doc){
    if(!err){
      if(!doc){
        let imgList = req.body.imgList;
        imgList.forEach(item=>{
          let imgData = item.raw.src
          let imgName = new Date().getTime()+item.name
          let base64Data = imgData.replace(/^data:image\/\w+;base64,/, '')
          let dataBuffer = Buffer.from(base64Data, 'base64')
          console.log(dataBuffer)
          fs.writeFile('./public/img/'+imgName, dataBuffer, function(err) {
            if (err) {
              res.send({
                code: 201,
                msg: "图片上传失败！"
              });
            }else{
              fs.readFile('./public/img/'+imgName, "binary", function(err, file) {
                if (err) {
                  console.log(err);
                  return;
                } else {
                  console.log(file)
                }

              });
            }
          })

        })
        return false
        let newGoods = {
          name: req.body.name,
          detail: req.body.detail,
          imgList: req.body.imgList,
          price: req.body.price,
          trademark: req.body.trademark,
          serialNumber: req.body.serialNumber,
          memberPrice: req.body.memberPrice,
          createTime: req.body.createTime,
        };
        goodsMethod.save(newGoods, function (err){
          if(!err){
            res.send({
              code: 200,
              msg: "新增成功！"
            });
          }
        });
      }else{
        res.send({
          code: 201,
          msg: "商品名字已被占用！"
        });
      }
    }
  });
};


// 获取商品列表
exports.getGoodsList = function (req, res){
  goodsMethod.findAll(function (err, doc) {
    if(!err){
      res.send({
        code: 200,
        data: doc,
        msg: "查询成功！"});
    }
    else{
      res.send({
        code: 201,
        msg: "查询失败！"});
    }
  })
};

// 删除
exports.delGoods = function (req, res){
  let id = req.body.id
  // 根据id查询单条数据
  goodsMethod.findOne({_id: req.body.id},function (err, doc) {
    if(!err && doc){
      let par = {
        _id: id
      }
      // 删除单条数据
      goodsMethod.remove(par,function (err, doc) {
        if(!err){
          res.send({
            code: 200,
            msg: "删除成功！"});
        }else{
          res.send({
            code: 200,
            msg: "删除失败！"});
        }
      })
    }
  })
};

// 修改
exports.editGoods = function (req, res) {
  let newGoods = {
    name: req.body.name,
    detail: req.body.detail,
    imgList: req.body.imgList,
    price: req.body.price,
    trademark: req.body.trademark,
    serialNumber: req.body.serialNumber,
    memberPrice: req.body.memberPrice,
    createTime: req.body.createTime,
  };
  // 查询单个数据
  goodsMethod.findOne({_id: req.body._id},function (err, doc) {
    if(!err && doc){
      // 更新单个数据
      goodsMethod.update({_id:req.body._id},newGoods,function (err) {
        if(!err){
          res.send({
            code: 200,
            msg: "修改成功！"});
        }
        else{
          res.send({
            code: 201,
            msg: "修改失败！"});
        }
      })
    }
  })
}

// 查询单个商品详情
exports.getGoodsDetail = function (req, res) {
  // 查询单个数据
  goodsMethod.findOne({_id: req.body.id},function (err, doc) {
    if(!err && doc){
      if(!err){
        res.send({
          code: 200,
          data: doc,
          msg: "查询成功！"});
      }
      else{
        res.send({
          code: 201,
          msg: "查询失败！"});
      }
    }
  })
}
